home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / qndxr.2 ƒ / misc_utils.2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-11-03  |  4.2 KB  |  170 lines  |  [TEXT/KAHL]

  1. /* file "misc_utils.c" -- miscellaneous utilities for the qndxr project...
  2.  * by ^z -- 870821-...
  3.  */
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <unix.h>
  8. #include <storage.h>
  9. #include <ctype.h>
  10. #include <proto.h>
  11. #include "qndxr.2.h"
  12.  
  13. /* this function returns a value for the size of the internal buffers,
  14.  * zbufsiz, and it also takes care of setting the other global parameters,
  15.  * keep_all_punct, keep_embedded_punct, and keep_special_chars,
  16.  * which govern how punctuation and special characters are handled.
  17.  * They are set based on flags such as -e, -a, and -s in the input line.
  18.  * The input parameters are taken one after another, and any that do
  19.  * not convert to a nonzero number are scanned for the letters "e", "a", and
  20.  * "s".  If a parameter is not reset, the default is to leave keep_all_punct,
  21.  * keep_embedded_punct, and keep_special_chars as FALSE.
  22.  * The default memory size is DEFAULT_MEMSIZ, set in the header file.
  23.  */
  24.  
  25. long set_params (argc, argv)
  26.   int argc;
  27.   char *argv[];
  28.   {
  29.     int i;
  30.     void set_parser_options();
  31.     long zb = 0, n, atol(), set_zbufsiz();
  32.     
  33.     for (i = 2; i < argc; ++i)
  34.       {
  35.         n = atol (argv[i]);
  36.         if (n != 0)
  37.             zb = set_zbufsiz (n);
  38.         else
  39.             set_parser_options (argv[i]);
  40.       }
  41.  
  42.     if (zb == 0)
  43.         zb = set_zbufsiz (DEFAULT_MEMSIZ);
  44.     
  45.     return (zb);
  46.   }
  47.  
  48.  
  49.  
  50. /* determine how big we should make the big buffers -- want to be sure
  51.  * to have room for at least 2*NMERGE+2 of them at one time in memory,
  52.  * so that the N-way merge function can hold all the input files
  53.  * (2N) plus the output files (2).
  54.  *
  55.  * NOTE that the chosen buffer size must be a multiple of both sizeof(long)
  56.  * and sizeof(KEY_REC); I ensure that very simply in what follows by
  57.  * a little modular arithmetic.  I also take care of the sign and of the
  58.  * requirement that the buffer size be non-zero -- thus, UNIX aficionados
  59.  * can precede the parameter with a "-" with no ill effects.  I have put in
  60.  * various safeguards against picking illegal buffer sizes, along with an
  61.  * ultimate safety net small minimum value for zbufsiz.
  62.  */
  63.  
  64. long set_zbufsiz (zb)
  65.   long zb;
  66.   {
  67.     char *testb;
  68.  
  69.     if (zb < 0)
  70.         zb = -zb;
  71.     
  72.     zb /=  (2 * NMERGE + 2);
  73.     zb = zb - zb % (sizeof(KEY_REC) * sizeof(long));
  74.     
  75.     if (zb < sizeof(KEY_REC) * sizeof(long))
  76.         zb = sizeof(KEY_REC) * sizeof(long);
  77.  
  78.     DEBUG ("--Checking for memory to support buffer size zb=%ld\n", zb);
  79.     testb = make_buf ((2 * NMERGE + 2) * zb);
  80.     free (testb);
  81.     
  82.     return (zb);
  83.   }
  84.  
  85.  
  86. /* function to set the parser options based on a character string ...
  87.  * 'a' turns on keep_all_punct, 'e' turns on keep_embedded_punct,
  88.  * and 's' turns on keep_special_chars
  89.  */
  90.  
  91. void set_parser_options (str)
  92.   char *str;
  93.   {
  94.     extern int keep_all_punct, keep_embedded_punct, keep_special_chars;
  95.  
  96.     while (*str)
  97.       {
  98.         if (*str == 'a')
  99.           {
  100.             keep_all_punct = TRUE;
  101.             printf ("All punctuation characters will be kept.\n");
  102.           }
  103.         else if (*str == 'e')
  104.           {
  105.             keep_embedded_punct = TRUE;
  106.             printf ("Embedded punctuation characters will be kept.\n");
  107.           }
  108.         else if (*str == 's')
  109.           {
  110.             keep_special_chars = TRUE;
  111.             printf ("Special characters will be kept.\n");
  112.           }
  113.         
  114.         ++str;
  115.       }
  116.   }
  117.   
  118.  
  119.  
  120. /* function to check for user interruption of operations (for use in the
  121.  * Macintosh version of this program only) ... call SystemTask() to give
  122.  * desk accessories a bit of time, and then check for non-null events
  123.  * with GetNextEvent() ... if something comes along (a mouse click, or key
  124.  * press, or whatever) let the user choose to exit the program or to
  125.  * carry on ....
  126.  */
  127.  
  128. #ifdef LIGHTSPEED
  129.  
  130. void check_interrupt ()
  131.   {
  132.     EventRecord myEvent;
  133.     char cmd[256], *gets();
  134.     void exit();
  135.  
  136.     SystemTask ();
  137.     if (GetNextEvent (everyEvent, &myEvent))
  138.       {
  139.         fprintf (stderr, "\Quit indexing now?\n");
  140.         gets (cmd);
  141.         if (cmd[0] == 'y' || cmd[0] == 'Y')
  142.             exit (0);
  143.       }
  144.   }
  145.  
  146. #endif
  147.  
  148.  
  149. /* a very simple function to return the size of a file ... leave the file
  150.  * pointer positioned at wherever it was when the routine was entered....
  151.  * should work fine with stdio methods, but not guaranteed compatible when
  152.  * mixed in with Mac-specific FSRead() function!
  153.  */
  154.  
  155. long file_size (fp)
  156.   FILE *fp;
  157.   {
  158.     long fpos, result, ftell();
  159.     
  160.     DEBUG ("--finding the size of file fp=%ld\n", fp);
  161.     fpos = ftell (fp);
  162.     fseek (fp, 0L, 2);
  163.     result = ftell (fp);
  164.     fseek (fp, fpos, 0);
  165.     return (result);
  166.   }
  167.  
  168.  
  169.  
  170.